# -*- encoding: utf-8 -*-
'''
Filename         :2_addTwoNumbers.py
Description      :
Time             :2022/08/02 09:48:15
Author           :daiyizheng
Email            :387942239@qq.com
Version          :1.0
'''

from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(-1)
        carry = 0
        curr = dummy
        while l1 is not None or l2 is not None:
            x1 = l1.val if l1 is not None else 0
            x2 = l2.val if l2 is not None else 0
            y = x1+x2+carry
            carry = y//10
            curr.next = ListNode(y%10)
            if l1 is not None:
                l1 = l1.next
            if l2 is not None:
                l2 = l2.next
            curr = curr.next
        if carry:
            curr.next = ListNode(carry)
        return dummy.next
            
